home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12798 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  51 lines

  1. Path: hobbes.sco.COM!md
  2. From: md@sco.COM (Michael Davidson)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: help with strcmp
  5. Date: 2 Apr 1996 21:21:43 GMT
  6. Organization: The Santa Cruz Operation, Inc.
  7. Message-ID: <4js5p7$q44@hobbes.sco.COM>
  8. References: <4jpiek$lp6@blaze.cs.jhu.edu>
  9. NNTP-Posting-Host: execsrvr.research.sco.com
  10. Cc: 
  11.  
  12.  
  13. In article <4jpiek$lp6@blaze.cs.jhu.edu>,
  14. John E. Davis <lasher@hops.cs.jhu.edu> wrote:
  15. >For some reason when I try to compare the strings in the following 
  16. >snippet I consistently get a core dump (running on a UNIX machine running 
  17. >Solaris).  Could anyone point out what may be going wrong?  I have run it 
  18. >through the debugger and that was no help at all for me.  here is the 
  19. >code snippet:
  20.  
  21. Ignoring the fact that you have declared main() incorrectly, and
  22. that you are not checking either argc (to make sure that argv[1]
  23. is legal) or the return value from the first fopen(), your problem
  24. appears to be that you are not checking for end-of-file correctly.
  25.  
  26. More specifically you test for end of file at the top of your
  27. loop, but *not* immediately after the call to fgets().
  28.  
  29. feof() can't predict the future - if tells you the *current*
  30. EOF status of the file, not what it *will* be after the next
  31. time you try to read the file (eg by calling fgets()).
  32.  
  33. Consider the limiting case of a 0 length file - immediately after
  34. the fopen() feof() will return FALSE, however the first call to
  35. fgets() will return NULL and subsequent calls to feof() will
  36. return TRUE. Your code, however will try to use the NULL pointer
  37. returned by the call to fgets() that actually hit end-of-file.
  38.  
  39.  
  40. You could fix this by rewriting your loop thus:
  41.  
  42.     while (fp = fgets(data, 40, handle)) {
  43.     n = 0;
  44.     
  45.     if ( strcasecmp(fp, "<action>\n") == 0)
  46.         parseAction(handle, dest); 
  47.  
  48.         ...
  49.     }
  50.  
  51.